﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Media3D;
using System.Drawing;


namespace PlanetsAndOrbs
{
    class Water : Object3D
    {
        private Vector3D filter_, emission_;
        private List<Vector3D> drops_ = new List <Vector3D>();

        public Water(int ripples, Color filter, Color emission, Random r)
        {
            filter_ = PixelRenderer.color2V3D(filter);
            emission_ = PixelRenderer.color2V3D(emission);
            normalizeColors(ref filter_, ref emission_);

            for (int i = 0; i < ripples; ++i)
                drops_.Add(new Vector3D((r.Next(200) - 50) / 10.0f, 0, (r.Next(200) - 50) / 10.0f));
        }


        public override double distance(Vector3D origin, Vector3D ray)
        {
            if (ray.Y >= 0)
                return -1;

            return -origin.Y / ray.Y;
        }


        public override bool trace(ref Vector3D origin, ref Vector3D ray, out Vector3D filter, out Vector3D emission)
        {
            Vector3D normal = new Vector3D(0, 1, 0);

            for (List <Vector3D>.Enumerator e = drops_.GetEnumerator(); e.MoveNext();)
            {
                Vector3D C = origin - e.Current;
                double way = C.Length;
                double amp = 1 / (C.LengthSquared + 1);
                C.Normalize();
                normal += C * (Math.Sin(2 * way) * amp);
            }
            normal.Normalize();

            double alpha = reflect(ref ray, normal);
            filter = filter_ * (1 - alpha);
            emission = emission_ * alpha;
            return false;
        }




    }
}
